<template>
  <a-date-picker v-if="!props.isNoRange" :placeholder="请选择日期" :format="props.valueFormat" :show-time="props.showTime || defaultShowTime" :showNow="false" @focus="dateBlur" 
    :disabledDate="getDisabledDate" :disabledTime="disabledRangeTime"></a-date-picker>
  <a-range-picker v-else :placeholder="['开始时间', '结束时间']" :allowClear="props.allClear ? false : true" :valueFormat="props.valueFormat" :show-time="props.showTime || defaultShowTime2"
    :disabledDate="getDisabledDate" :disabledTime="disabledRangeTime" @change="change" @focus="dateBlur" ></a-range-picker>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted, computed } from "vue"
import { dateRangePicker } from "./pickerConfig/props"
import dayjs, { Dayjs } from 'dayjs';
import { emitWarning } from "process";
import moment from 'moment';
type RangeValue = [Dayjs, Dayjs];
//props
const props = defineProps(dateRangePicker);
// emit 
const emit = defineEmits(['change'])
// data
const format = ref(props.valueFormat);
const defaultShowTime = ref<any>({ defaultValue: dayjs(dayjs().add(5, 'minute'),'HH:mm:ss')});
const defaultShowTime2 = ref<any>({defaultValue: [dayjs(dayjs().add(5, 'minute'),'HH:mm:ss'), dayjs(dayjs().add(5, 'minute'),'HH:mm:ss')]});
//methods
// const disabledDate = (current: any) => {
//   let result = false;
//   if (current > dayjs().add(0, 'days')) {
//     result = true;
//   }
//   return result;
// }
// const getDisabledDate = { // 
//   type:2,
//   startDays:0,
// }
const dateBlur = (val: any) => {
  if(props.disabledInfo && props.disabledInfo.type == 7) {
    defaultShowTime.value.defaultValue = dayjs(dayjs().add(5, 'minute'),'HH:mm:ss');
    defaultShowTime2.value.defaultValue = [dayjs(dayjs().add(5, 'minute'),'HH:mm:ss'), dayjs(dayjs().add(5, 'minute'),'HH:mm:ss')];
  }
}
const range = (start: any, end: any) => {
  const result = [];
  for (let i = start; i < end; i++) {
    result.push(i);
  }
  return result;
}
const disabledRangeTime = (dates: any,) => {
  if(props.disabledInfo && props.disabledInfo.type == 7) { // 当前时间+5分钟不可选
    if(dates) {
    let today = moment().date();
    if(today == dates.date()) {
      let hour = Number(moment().hours())
      let minute = Number(moment().minutes())
      let finalHour: number, finalMinute: number; 
      if(dates.hour() > hour) {
          finalHour = hour
          finalMinute = 0
      } else {
        if(dates.minute() > 58) {
          finalHour = hour + 1
          finalMinute = 0
        } else {
          finalHour = hour
          finalMinute = minute + 5
        }
      }
      return{
        disabledHours: () => range(0, finalHour),
        disabledMinutes: () => range(0, finalMinute)
      }
    } else if(today > dates.date()) {
      return {
        disabledHours: () => range(0, 24),
        disabledMinutes: () => range(0, 60),
        disabledSeconds: () => range(0, 60)
      }
    }
  } else {
    return {
      disabledHours: () => range(0, 24),
      disabledMinutes: () => range(0, 60),
      disabledSeconds: () => range(0, 60),
    }
  }
  }else {
    return false
  }
};
const getDisabledDate = (val: any) => {
  if (props.disabledInfo && props.disabledInfo.type == 1) {
    // 当前时间之前不可选(不包含当前日期)
    return val < dayjs().add(props.disabledInfo.startDays || 0, 'days');
  } else if (props.disabledInfo && props.disabledInfo.type == 2) {
    // 当前时间之后不可选（不包含当前日期）
    return val > dayjs().add(props.disabledInfo.startDays || 0, 'days');
  } else if (props.disabledInfo && props.disabledInfo.type == 3) {
    // 当前区间内不可选
    return !(val < dayjs().add(props.disabledInfo.endDays, 'days') && val > dayjs().add(props.disabledInfo.startDays, 'days'));
  } else if (props.disabledInfo && props.disabledInfo.type == 4) {
    // 当前区间外不可选
    let startTime = dayjs(props.disabledInfo.start, format.value);
    let endTime = dayjs(props.disabledInfo.end, format.value);
    return val.valueOf() < startTime.valueOf() || endTime.valueOf() <= val.valueOf();
  } else if (props.disabledInfo && props.disabledInfo.type == 6) {
    // 只有周末可以选
    let oldStr = new Date(val);
    let sd = oldStr.getDay() == 0 ? 7 : oldStr.getDay();
    if (sd == 6 || sd == 7) return false;
    else return true;
  } else if(props.disabledInfo && props.disabledInfo.type == 7) {
    // return val < dayjs().add(props.disabledInfo.startDays || -1, 'days');
    return val < dayjs().endOf('day').add(-1, 'days');
  } else { 
    return false;
  }
}

const change = (val: any) => {
  emit('change', val)
}
</script>

<style lang="less" scoped></style>